package com.zhuzhe.blog.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhuzhe.blog.base.JsonData;
import com.zhuzhe.blog.base.ResponseData;
import com.zhuzhe.blog.convert.Convert;
import com.zhuzhe.blog.system.entity.SysRole;
import com.zhuzhe.blog.system.requestDto.SysRoleDto;
import com.zhuzhe.blog.system.requestDto.SysRolePageDto;
import com.zhuzhe.blog.system.responseVo.SysRoleListVo;
import com.zhuzhe.blog.system.service.ISysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 角色信息表 前端控制器
 * </p>
 *
 * @author u53
 * @since 2020-10-13
 */
@Api(value = "角色管理接口", description = "角色管理接口，提供角色的增删改查")
@RestController
@RequestMapping("/sys-role")
public class SysRoleController {


    @Autowired
    private ISysRoleService sysRoleService;

    /**
     * 分页查询角色列表
     *
     * @param sysRolePageDto
     * @return com.zhuzhe.blog.base.ResponseData<com.baomidou.mybatisplus.core.metadata.IPage < com.zhuzhe.blog.system.responseVo.SysRoleListVo>>
     * @throws
     * @author whh
     * @date 2020/10/17 14:09
     */
    @ApiOperation("根据角色名称分页查询角色列表")
    @PostMapping("/seach")
    public ResponseData<IPage<SysRoleListVo>> queryPage(@RequestBody SysRolePageDto sysRolePageDto) {
        return sysRoleService.queryPage(sysRolePageDto);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("根据角色id删除角色及角色和菜单关联，角色和用户关联")
    @ApiImplicitParam(name = "id", value = "角色id", required = true)
    public ResponseData<String> deleteMenu(@PathVariable("id") String id) {

        return sysRoleService.deleteById(id);
    }


    @PostMapping("/save")
    @ApiOperation("新增角色信息接口")
    @ApiImplicitParam(name = "sysRoleDto", value = "角色信息", required = true)
    public ResponseData<Integer> save(@RequestBody SysRoleDto sysRoleDto) {
        SysRole sysRole = new SysRole();
        Convert.convertType(sysRoleDto, sysRole);
        return JsonData.success(sysRoleService.save(sysRole));
    }


    @GetMapping("/view/{id}")
    @ApiOperation("查询角色详情接口")
    @ApiImplicitParam(name = "id", value = "角色id", required = true)
    public ResponseData<SysRoleListVo> viewMenu(@PathVariable("id") String id) {
        SysRole sysRole = sysRoleService.getById(id);
        SysRoleListVo sysRoleListVo = new SysRoleListVo();
        Convert.convertType(sysRole, sysRoleListVo);
        return JsonData.success(sysRoleListVo);
    }

    @PutMapping("/update")
    @ApiOperation("修改角色信息接口")
    public ResponseData<Integer> updateMenu(@RequestBody SysRoleDto sysRoleDto) {

        SysRole sysRole = new SysRole();
        Convert.convertType(sysRoleDto, sysRole);
        sysRole.setUpdateDate(new Date());
        sysRoleService.updateById(sysRole);
        return JsonData.success(sysRole.getId());
    }

    @ApiOperation("查询角色下授权菜单ids")
    @ApiImplicitParam(name = "id", value = "角色id", required = true)
    @GetMapping("/{id}/menu/ids")
    public ResponseData<List<String>> findMenuIdsById(@PathVariable("id") String id) {
        return sysRoleService.findMenuIdsById(id);
    }


    @PostMapping("/{id}/menu/saveRoleMenu")
    @ApiOperation("授权角色权限菜单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "角色id", required = true),
            @ApiImplicitParam(name = "menuIds", value = "菜单ids", readOnly = true)
    })
    public ResponseData<Integer> saveRoleMenu(@PathVariable("id") String id,
                                              @RequestBody List<String> menuIds) {

        return sysRoleService.saveRoleMenu(id,menuIds);
    }


}
